package com.coocaa.ops.admin.v1.panel.biz.impl;


import com.coocaa.ops.admin.v1.panel.biz.IPanelBlockRlsBiz;
import com.coocaa.ops.admin.v1.panel.mapper.IPanelBlockRlsMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.panel.constant.SubPanelVersionConstants;
import com.coocaa.ops.common.core.biz.panel.entity.PanelBlockRlsEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description 板块和推荐位关联表-biz层接口实现
 * @date 2020-06-13 11:06:59
 */
@Service
public class  PanelBlockRlsBizImpl extends BaseBizImpl<IPanelBlockRlsMapper, PanelBlockRlsEntity> implements IPanelBlockRlsBiz {
    @Override
    public List<PanelBlockRlsEntity> listPanelBlockRls(Integer panelId,Integer panelVersion ) {
        QueryTemplate<PanelBlockRlsEntity> query = QueryTemplate.getEqQueryTemplate("flag",1);
        query.lambda().eq(PanelBlockRlsEntity::getPanelId, panelId)
                .eq(PanelBlockRlsEntity::getPanelVersion, panelVersion);
        return getMapper().selectList(query);
    }


    @Override
    public void deleteRls(Integer id, Integer version) {
        QueryTemplate<PanelBlockRlsEntity> queryTemplate = this.getPanelBlockRlsEntityQuery(id, version);
        if(getMapper().selectCount(queryTemplate) > 0){
            getMapper().delete(queryTemplate);
        }
    }

    @Override
    public void logicDelRls(Integer id, Integer version) {
        QueryTemplate<PanelBlockRlsEntity> queryTemplate = this.getPanelBlockRlsEntityQuery(id, version);
        PanelBlockRlsEntity update = new PanelBlockRlsEntity();
        update.setFlag(ResourceStatusEnums.OFF_LINE.getStatus());
        getMapper().update(update, queryTemplate);
    }

    @Override
    public List<Integer> queryBlockIdList(Integer id, Integer version) {
        QueryTemplate<PanelBlockRlsEntity> queryTemplate = this.getPanelBlockRlsEntityQuery(id, version);
        List<PanelBlockRlsEntity> list =getMapper().selectList(queryTemplate);
        return this.convert2BlockIdList(list);
    }

    @Override
    public List<Integer> queryBlockIdListForGroupPanel(List<Integer> subPanelIdList) {
        QueryTemplate<PanelBlockRlsEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag",1);
        queryTemplate.lambda().in(PanelBlockRlsEntity::getPanelId, subPanelIdList)
                .in(PanelBlockRlsEntity::getPanelVersion, SubPanelVersionConstants.SUB_PANEL_VERSION);
        List<PanelBlockRlsEntity> list =getMapper().selectList(queryTemplate);
        return this.convert2BlockIdList(list);
    }

    /**
     * 获取推荐位id
     * @param list
     * @return
     */
    private List<Integer> convert2BlockIdList( List<PanelBlockRlsEntity> list){
        List<Integer> result = InstanceUtil.newArrayList();
        if(CollectionUtils.isEmpty(list)){
            return result;
        }
        for (PanelBlockRlsEntity entity : list){
            if(null != entity && null != entity.getBlockId()){
                result.add(entity.getBlockId());
            }
        }
        return result;
    }

    private QueryTemplate<PanelBlockRlsEntity>  getPanelBlockRlsEntityQuery(Integer id, Integer version){
        QueryTemplate<PanelBlockRlsEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(PanelBlockRlsEntity::getPanelId, id)
                .eq(PanelBlockRlsEntity::getPanelVersion, version);
        return queryTemplate;

    }
}
